/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import etcore.webapp.grouptalk.common.object.Directory;
import etcore.webapp.grouptalk.common.object.GroupDirectoryRelationship;

/**
 * An instance of DirectoryDAO is a data access object used to access Directory,
 * which contains query and update operations
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class DirectoryDAO extends BaseDAO {

	@Override
	public void deleteSingle(Object persistentInstance) {
		Session session = HibernateSessionFactory.getSession();
		if (persistentInstance instanceof Directory) {
			GroupDirectoryRelationshipDAO relationdao = new GroupDirectoryRelationshipDAO();

			List<GroupDirectoryRelationship> persistentInstanceList = relationdao
					.findByProperty("did",
							((Directory) persistentInstance).getId());
			relationdao.deleteList(persistentInstanceList);

		}

		session.delete(persistentInstance);

	}

	/**
	 * Find instances of Directory
	 * 
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return Directory instances
	 */
	public List<Directory> findAll(int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		sqlStringBuffer.append(" order by up");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Directory> results = queryObject.list();
		return results;

	}

	/**
	 * Find the count of Directory instances
	 * 
	 * @return the count of Directory instances
	 */
	public long findAllSize() {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select count(*) from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		Object results = queryObject.uniqueResult();
		return (results == null) ? 0 : ((Number) results).longValue();

	}

	/**
	 * Find Directory instances which contain the group
	 * 
	 * @param groupID
	 *            group id
	 * @return Directory instances which contain the group
	 */
	public List<Directory> findByGid(Integer groupID) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		sqlStringBuffer
				.append(" where  id in (select did  from GroupDirectoryRelationship where gid=? )");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, groupID);
		@SuppressWarnings("unchecked")
		List<Directory> results = queryObject.list();
		return results;

	}

	/**
	 * Find Directory instance by id
	 * 
	 * @param id
	 *            directory id
	 * @return Directory instance
	 */
	public Directory findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		Directory instance = (Directory) session.get(Directory.class.getName(),
				id);
		return instance;

	}

	/**
	 * Find the first directory instance of the input name
	 * 
	 * @param name
	 *            directory name
	 * @return Directory instance
	 */
	public Directory findByName(String name) {
		List<Directory> resultList = findByProperty("name", name, 0, 1);
		if (resultList != null && resultList.size() == 1)
			return (Directory) resultList.get(0);
		return null;
	}

	/**
	 * Find Directory instances which belong to the parent platform
	 * 
	 * @param parent
	 *            the parent platform code
	 * @return Directory instances which belong to the parent platform
	 */
	public List<Directory> findByParent(Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		sqlStringBuffer.append(" where parent=? order by up");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, parent);
		@SuppressWarnings("unchecked")
		List<Directory> results = queryObject.list();
		return results;

	}
	public List<Directory> findLocalDirsByParent(Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		sqlStringBuffer.append(" where local=1 and parent=? order by up");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, parent);
		@SuppressWarnings("unchecked")
		List<Directory> results = queryObject.list();
		return results;

	}
	public List<Directory> findEnglishDirsByParent(Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		sqlStringBuffer.append(" where en=1 and parent=? order by up");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, parent);
		@SuppressWarnings("unchecked")
		List<Directory> results = queryObject.list();
		return results;

	}
	/**
	 * Find Directory instances which belong to the parent platform
	 * 
	 * @param parent
	 *            the parent platform code
	 * @param begin
	 *            start position
	 * @param length
	 *            the count of instances
	 * @return Directory instances
	 */
	public List<Directory> findByParent(Integer parent, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		sqlStringBuffer.append(" where parent=? order by up");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, parent);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Directory> results = queryObject.list();
		return results;

	}

	/**
	 * Find Directory instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return Directory instances
	 */
	public List<Directory> findByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append("= ? order by up");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);
		@SuppressWarnings("unchecked")
		List<Directory> results = queryObject.list();
		return results;

	}

	/**
	 * Find Directory instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @param begin
	 *            start position
	 * @param length
	 *            the count of instances
	 * @return Directory instances
	 */
	public List<Directory> findByProperty(String propertyName, Object value,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append("= ? order by up");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Directory> results = queryObject.list();

		return results;

	}

	/**
	 * Find English Directory instances which belong to the parent platform
	 * 
	 * @param parent
	 *            parent platform code
	 * @return Directory instances
	 */
	public List<Directory> findEnglishDirectoriesByParent(Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		sqlStringBuffer.append(" where parent=? and en=1 order by up");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, parent);
		@SuppressWarnings("unchecked")
		List<Directory> results = queryObject.list();
		return results;

	}

	/**
	 * Find Directory instances of local language which belong to the parent
	 * platform
	 * 
	 * @param parent
	 *            parent platform code
	 * @return Directory instances
	 */
	public List<Directory> findLocalDirectoriesByParent(Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		sqlStringBuffer.append(" where parent=? and local=1 order by up");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, parent);
		@SuppressWarnings("unchecked")
		List<Directory> results = queryObject.list();
		return results;

	}

	/**
	 * Find the count of Directory instances which belong to the parent platform
	 * 
	 * @param parent
	 *            parent platform code
	 * @return the count of Directory
	 */
	public long findSizeByParent(Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select count(*) from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		sqlStringBuffer.append(" where parent=? ");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, parent);
		Object results = queryObject.uniqueResult();
		return (results == null) ? 0 : ((Number) results).longValue();

	}

	/**
	 * Find Directory instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return the count of Directory instances
	 */
	public long findSizeByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select count(*) from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append("= ?");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);
		Object results = queryObject.uniqueResult();
		return (results == null) ? 0 : ((Number) results).longValue();

	}

	/**
	 * Get parent platform code
	 * 
	 * @param id
	 *            Directory id
	 * @return parent platform code
	 */
	public int getParent(Integer id) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select parent from ");
		sqlStringBuffer.append(Directory.class.getSimpleName());
		sqlStringBuffer.append(" where id=? ");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, id);
		Object results = queryObject.uniqueResult();
		return (results == null) ? 0 : ((Number) results).intValue();

	}

}